<template>
    <el-row :gutter="gutter" class="upload-container"> <!--16:9添加cover-map-->
        <el-col v-for="(item, index) in uploadedImgs" :key="item" :span="span">
            <div class="layout-block" :style="{marginBottom: gutter+'px'}">
                <div class="layout-item">
                    <video v-if="media === 'video'" :src="item" controls class="uploaded-img" />
                    <img v-if="media === 'image'" :src="item" alt="" class="uploaded-img">
                    <div class="uploaded-tools">
                        <i class="el-icon-zoom-in" @click.stop="imgView(item)" />
                        <i class="el-icon-delete" @click.stop="imgDelete(index)" />
                        <div class="tools-move">
                            <i class="el-icon-arrow-left" @click.stop="imgMove(index,-1)" />
                            <i class="el-icon-arrow-right" @click.stop="imgMove(index,1)" />
                        </div>
                    </div>
                </div>
            </div>
        </el-col>
        <el-col v-show="max > 0 ? uploadedImgs.length < max : true" :span="span">
            <el-upload
                :action="uploadMediaUrl"
                :accept="`${media}/*`"
                :show-file-list="false"
                :on-success="uploadSuccess"
                :on-error="uploadError"
                :before-upload="uploadBefore"
                :disabled="disabled"
            >
                <div class="layout-block">
                    <div v-loading="uploading" class="layout-item upload-btn">
                        <i class="el-icon-plus" />
                        <span v-if="size" class="upload-size">{{ $t('global.recomSize') }}：{{ size }}</span>
                        <!--<span class="upload-size">{{ $t('global.fileSize') }}：2M</span>-->
                    </div>
                </div>
            </el-upload>
        </el-col>
    </el-row>
</template>

<script>
import { arrMove } from '@/utils'
import { uploadMediaUrl } from '@/api/admin/upload'

export default {
    name: 'UploadMediaList',
    props: {
        gutter: {
            type: Number,
            default: 20 // 间隔 px
        },
        span: {
            type: Number,
            default: 4 // 分栏 1/24
        },
        value: {
            type: String,
            default: ''
        },
        media: {
            type: String,
            default: 'image' // video
        },
        disabled: {
            type: Boolean,
            default: false
        },
        size: {
            type: String,
            default: '' // 1920*1080
        },
        max: {
            type: Number,
            default: 0 // 最大个数
        }
    },
    data: function() {
        return {
            uploadMediaUrl,
            uploadedImgs: [],
            dialogVisible: false,
            dialogImageUrl: '',
            uploading: false
        }
    },
    computed: {
    },
    watch: {
        value() {
            this.uploadedImgs = this.value ? this.value.split(';') : [];
        },
        uploadedImgs(val) {
            this.$emit('input', this.uploadedImgs.join(';'));
        }
    },
    created() {
        this.uploadedImgs = this.value ? this.value.split(';') : [];
    },
    methods: {
        uploadBefore(event, file, fileList) {
            if (this.uploading) return false;
            /*const fileSize = event.size / 1024
            if (fileSize > 2048) {
                console.log('大小', fileSize);
                this.$message.error(this.$t('tips.imgSize'));
                return false
            }*/
            this.uploading = true;
        },
        uploadError(err) {
            console.log(err);
            this.uploading = false;
        },
        uploadSuccess(res) {
            this.uploading = false;
            this.uploadedImgs.push(res.data);
        },
        imgView(url) {
            window.open(url);
        },
        imgDelete(index) {
            this.uploadedImgs.splice(index, 1);
        },
        imgMove(index, step) {
            this.uploadedImgs = arrMove(this.uploadedImgs, index, step)
        }
    }
}
</script>

<style lang="scss" scoped>
.upload-container {
    .layout-block {
        position: relative;
        width: 100%;
        height: 0;
        padding-bottom: 100%;
    }

    .layout-item {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 4px;
        background-color: #999;
        overflow: hidden;

        .uploaded-img {
            display: block;
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
        .uploaded-tools {
            display: flex;
            align-items: center;
            justify-content: center;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            font-size: 20px;
            color: #e0e0e0;
            background-color: rgba(0, 0, 0, .5);
            opacity: 0;
            visibility: hidden;
            transition: .3s;

            i {
                padding: 5px;
                cursor: pointer;
                transition: .3s;

                &:hover {
                    color: #fff;
                }
            }

            .tools-move {
                position: absolute;
                bottom: 0;
                left: 0;
            }
        }

        &:hover {
            .uploaded-tools {
                opacity: 1;
                visibility: visible;
            }
        }
    }

    .upload-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border: 1px dashed #d9d9d9;
        background-color: #fbfdff;
        border-radius: 4px;
        transition: .3s;

        i {
            position: relative;
            font-size: 28px;
            color: #8c939d;
            transition: .3s;
            z-index: 5;
        }

        .upload-size {
            position: relative;
            font-size: 12px;
            line-height: 1;
            color: #999;
            z-index: 5;
        }

        .uploaded-single{
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: contain;
            z-index: 1;
            transition: .3s;
        }
        &:hover {
            background-color: #e6ebf1;
            border-color: #1d38FF;
            i{
                color: #1d38FF;
            }
        }
        &.has-img {
            i {
                color: #fff;
                opacity: 0;
                filter: drop-shadow(0px 0px 4px black);
                transition: .3s;
            }

            .upload-size {
                color: #fff;
                opacity: 0;
                text-shadow: 0 0 4px black;
                transition: .3s;
            }

            &:hover {
                i {
                    opacity: 1;
                }
                .upload-size {
                    opacity: 1;
                }
            }
        }
    }

    ::v-deep {
        .el-upload {
            display: block;
        }
    }

    &.cover-map{
        .layout-block {
            padding-bottom: 56.25%;
        }
        .uploaded-single{
            object-fit: cover !important;
        }
    }
}
</style>
